﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command.MenuaLL;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AuthCenter_HuangJingtao.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuALLController : ControllerBase
    {
        IMediator mediator;
        DbdataContext context;
        public MenuALLController(IMediator mediator, DbdataContext context)
        {
            this.mediator = mediator;
            this.context = context;
        }

        [HttpPost]
        public async Task<ApiResult<int>> AddMenus(Menus menus)
        {
            try
            {
                AddMenusCommand AddMenusCommand = new AddMenusCommand()
                {
                    menus = menus
                };
                return await mediator.Send(AddMenusCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpGet]
        public async Task<ApiResult<List<Menus>>> ShowMenuAll()
        {
            try
            {
                ShowMenuCommand ShowMenuCommand = new ShowMenuCommand();
                return await mediator.Send(ShowMenuCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpPost]
        public async Task<ApiResult<int>> UpdateMenus(Menus menus)
        {
            try
            {
                UpdateMenusCommand UpdateMenusCommand = new UpdateMenusCommand()
                {
                    menus = menus
                };
                return await mediator.Send(UpdateMenusCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [HttpDelete]
        public async Task<ApiResult<int>> DelMenus(int Id)
        {
            try
            {
                DelMenusCommand DelMenusCommand = new DelMenusCommand()
                {
                    Id = Id
                };
                return await mediator.Send(DelMenusCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }
        [HttpGet]
        public async Task<ApiResult<Menus>> FirstMenus(int MenuId)
        {
            try
            {
                FirstMenusCommand FirstMenusCommand = new FirstMenusCommand()
                {
                    MenuId = MenuId
                };
                return await mediator.Send(FirstMenusCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public static List<TreeNode> BuildTree(List<(int Id, int? ParentId, string Label)> data, int? parentId = null)
        {
            return data
                .Where(d => d.ParentId == parentId)
                .Select(d => new TreeNode
                {
                    Id = d.Id,
                    Label = d.Label,
                    Children = BuildTree(data, d.Id) // 递归获取子节点
                })
                .ToList();
        }
        [HttpGet]
        public List<TreeNode> BuildTree()
        {
            var list = context.Set<Menus>().Where(c=>!c.Menu_IsDel).ToList();
            var data = new List<(int Id, int? ParentId, string Label)>();
            //{
            //    //(1, null, "权限菜单"),
            //    //(2, 1, "Child 1"),
            //    //(3, 1, "Child 2"),
            //    //(4, 2, "Grandchild 1"),
            //    //(5, 2, "Grandchild 2"),
            //};
            double cout=1;
            for (int i = 1; i <= list.Count; i++)
            {
                if (i == 1)
                {
                    data.Add((i, null, "权限菜单"));
                }
                else 
                {
                    data.Add((i, (int)(cout), list[i - 1].Menu_Name));
                    cout += 0.5;
                }
            }
            return BuildTree(data);
        }
        [HttpPost]
        public async Task<ApiResult<int>> AddRoleMenus(RoleMenus RoleMenus)
        {
            try
            {
                AddRoleMenusCommand AddRoleMenusCommand = new AddRoleMenusCommand() 
                {
                    RoleMenus = RoleMenus
                };
                return await mediator.Send(AddRoleMenusCommand);
            }
            catch (Exception)
            {
                throw;
            }
        }
        [HttpGet]
        public async Task<ApiResult<RoleMenus>> FistRoleMenus(int Role_Id)
        {
            try
            {
                FirstRoleMenusCommand FirstRoleMenusCommand = new FirstRoleMenusCommand() 
                {
                    Role_Id = Role_Id
                };
                return await mediator.Send(FirstRoleMenusCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        public async Task<ApiResult<List<ApiTree>>> SeRoleTree()
        {
            try
            {
                SeTreeCommand SeTreeCommand = new SeTreeCommand();
                return await mediator.Send(SeTreeCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
